package com.wingsoft.framework.web.service;

import com.wingsoft.common.constant.CacheConstants;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.wingsoft.common.constant.Constants;
import com.wingsoft.common.constant.UserConstants;
import com.wingsoft.common.core.domain.entity.SysUser;
import com.wingsoft.common.core.domain.model.RegisterBody;
import com.wingsoft.common.core.redis.RedisCache;
import com.wingsoft.common.exception.user.CaptchaException;
import com.wingsoft.common.exception.user.CaptchaExpireException;
import com.wingsoft.common.utils.MessageUtils;
import com.wingsoft.common.utils.SecurityUtils;
import com.wingsoft.common.utils.StringUtils;
import com.wingsoft.framework.manager.AsyncManager;
import com.wingsoft.framework.manager.factory.AsyncFactory;
import com.wingsoft.system.service.ISysConfigService;
import com.wingsoft.system.service.ISysUserService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 注册校验方法
 *
 * @author fyadmin
 */
@Component
public class SysRegisterService
{
    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysConfigService configService;

    @Autowired
    private RedisCache redisCache;

    /**
     * 注册
     */
    @Transactional
    public String register(RegisterBody registerBody) {
        String msg = "", nickname = registerBody.getNickname(), phone = registerBody.getPhone(),username = registerBody.getUsername(), code = registerBody.getCode(), password = registerBody.getPassword();
/*        if (ObjectUtils.isEmpty(code)) {
            return "验证码不能为空！";
        }
        boolean captchaOnOff = configService.selectCaptchaEnabled();
        if (captchaOnOff)
        {
            validateCaptcha(username, code, registerBody.getUuid());
        }*/
/*        if (ObjectUtils.isEmpty(phone)) {
            return "电话不能为空！";
        }*/
/*        String codePre = redisCache.getCacheObject("code-" + phone);
        if (ObjectUtils.isEmpty(codePre)){
            return "验证码不正确或已失效！";
        }
        if (!codePre.equals(codePre)) {
            return "验证码不正确或已失效！";
        }*/
/*        if (ObjectUtils.isEmpty(nickname)) {
            return "姓名不能为空！";
        }*/
        if (ObjectUtils.isEmpty(password)) {
            return "密码不能为空！";
        }
        if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
                || password.length() > UserConstants.PASSWORD_MAX_LENGTH) {
            return "密码长度必须在5到20个字符之间！";
        }
        SysUser sysUser = new SysUser();
        sysUser.setUserName(username);
        if (!userService.checkUserNameUnique(sysUser)) {
            return "保存用户'" + username + "'失败，注册用户账号已存在";
        }
/*        sysUser.setPhonenumber(phone);
        if (userService.checkPhoneUnique(sysUser)) {
            return "保存用户'" + username + "'失败，注册手机已存在";
        }*/
        sysUser.setUserName(username);
        sysUser.setNickName(username);
        sysUser.setPassword(SecurityUtils.encryptPassword(registerBody.getPassword()));
        sysUser.setUserType("02");
        userService.insertUser(sysUser);
        AsyncManager.me().execute(AsyncFactory.recordLogininfor(phone, Constants.REGISTER,
                MessageUtils.message("user.register.success")));
        return msg;
    }

    /**
     * 校验验证码
     *
     * @param username 用户名
     * @param code 验证码
     * @param uuid 唯一标识
     * @return 结果
     */
    public void validateCaptcha(String username, String code, String uuid)
    {
        String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + StringUtils.nvl(uuid, "");
        String captcha = redisCache.getCacheObject(verifyKey);
        redisCache.deleteObject(verifyKey);
        if (captcha == null)
        {
            throw new CaptchaExpireException();
        }
        if (!code.equalsIgnoreCase(captcha))
        {
            throw new CaptchaException();
        }
    }
}
